<template>
  <div class="personal-main">
    <div class="pmain-profile">
      <div class="pmain-welcome">
        <span class="fr">上次登录时间： {{ userIndexVO.lastLoginTime }} </span>
      </div>
      <div class="pmain-user">
        <div class="user-head">
          <span class="head-img">
            <span>
              <img
                :src="userIndexVO.headImg"
                style="width:88px;height:88px;z-index:0;"
              />
              <i class="headframe" style="z-index:0;"></i>
            </span>
          </span>
        </div>
        <div class="user-info">
          <ul>
            <li>
              用户名<span>{{ userIndexVO.name }}</span>
              <NuxtLink
                v-if="
                  userIndexVO.userType === 2 && userIndexVO.bindStatus === 1
                "
                to="/user/borrower"
              >
                立即借款
              </NuxtLink>
            </li>
            <li v-if="userIndexVO.bindStatus !== 1">
              您还未开通第三方支付账户，请
              <NuxtLink to="/user/bind">立即开通</NuxtLink>
              以确保您的正常使用和资金安全。
            </li>
          </ul>
        </div>
      </div>

      <div v-if="userIndexVO.bindStatus === 1" class="pmain-money">
        <ul>
          <li class="none">
            <span>
              <em>账户余额</em>
              <i class="markicon"></i>
            </span>
            <span class="truemoney">
              <i class="f26 fb">{{ userIndexVO.amount }}</i> 元
            </span>
          </li>
          <li>
            <span>
              <em>冻结金额</em>
              <i class="markicon"></i>
            </span>
            <span class="truemoney">
              <i class="f26 fb">{{ userIndexVO.freezeAmount }}</i
              >元
            </span>
          </li>
          <li>
            <NuxtLink to="/user/recharge">
              <el-button size="mini" type="danger">充值</el-button>
            </NuxtLink>
            <NuxtLink to="/user/withdraw">
              <el-button size="mini" type="success">提现</el-button>
            </NuxtLink>
          </li>
        </ul>
      </div>
    </div>

    <!-- 会员绑定手机号 选择用户类型 对话框 -->
    <div>
      <el-dialog
        title="完善个人信息"
        :visible.sync="dialogFormVisible"
        :show-close="false"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
      >
        <el-form>
          <el-form-item label="手机号" :label-width="formLabelWidth">
            <el-input v-model="bindVO.mobile" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="用户类型" :label-width="formLabelWidth">
            <el-select v-model="bindVO.userType" placeholder="请选择类型">
              <el-option label="借款人" :value="2"></el-option>
              <el-option label="投资人" :value="1"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="handleBind">
            确 定
          </el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      formLabelWidth: "120px",
      bindVO: { userType: 2 }, //会员绑定数据
      dialogFormVisible: false,
      userIndexVO: {},
    };
  },
  //使用的localStorage 是浏览器的API，created在nuxt服务器中 由服务器执行
  mounted() {
    //判断用户是否选择了用户类型 并且绑定了手机号
    let srbUser = localStorage.getItem("srb-user");
    if (srbUser) {
      srbUser = JSON.parse(srbUser);
      if (srbUser.userType === 0) {
        //弹出一个表单对话框，提示用户绑定手机号  选择用户类型
        this.dialogFormVisible = true;
        return;
      }
    }
    this.fetchUserData();
  },

  methods: {
    //绑定按钮事件函数
    handleBind() {
      if (this.bindVO.mobile) {
        if (this.bindVO.userType == 1 || this.bindVO.userType == 2) {
          let token = localStorage.getItem("srb-token");
          if (!token) {
            this.$message.warning("请先登录");
            return;
          }
          this.$axios({
            url: `/api/core/userInfo/auth/userInfo/${this.bindVO.mobile}/${this.bindVO.userType}`,
            method: "PUT",
            headers: { token },
          }).then((r) => {
            //r.data.data.item 代表更新后的srb-user的数据
            //可将它覆盖之前的srb-user
            localStorage.setItem("srb-user", JSON.stringify(r.data.data.item));
            //刷新当前页面
            window.location.reload();
          });
        }
      }
    },
    fetchUserData() {
      let token = localStorage.getItem("srb-token");
      if (!token) {
        this.$message.warning("请先登录");
        return;
      }
      this.$axios({
        url: "/api/core/userInfo/auth/indexData",
        method: "get",
        headers: { token }, // {'token':token}
      }).then((r) => {
        this.userIndexVO = r.data.data.item;
      });
    },
  },
};
</script>
